﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Tilver.Data;

namespace Weixin.MP.UI
{
    public class WeixinDataRouter : DbRouterBase
    {
        private static readonly object _lock = new object();
        private static readonly Dictionary<string, string> _routerCache = new Dictionary<string, string>();

        /// <summary>
        /// 路由方法创建数据访问类
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override DbDataBase RouteDataBase(Type type, object routeData = null)
        {
            var key = string.Format("{0}", type.FullName);
            if (!_routerCache.ContainsKey(key))
            {
                lock (_lock)
                {
                    if (!_routerCache.ContainsKey(key))
                    {
                        var connectionStringName = "WeixinMP";
                        //控制不同的类访问不同的数据库
                        if (type.FullName.Contains(".DAL.WeixinMP"))
                        {
                            connectionStringName = "WeixinMP";//配置文件中定义的数据库访问名称
                        }
                        _routerCache.Add(key, connectionStringName);
                    }
                }
            }
            return DbDataFactory.Create(_routerCache[key]);
        }
    }
    
}
